<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-row>
          <!-- 说明 -->
          <el-col :span="12">
            <span style="color:pink;font-size:12px">说明：目前支持学科和关键字条件筛选</span>
          </el-col>
          <!-- 新增试题 -->
          <el-col :span="12">
            <el-button style="float:right" size="small" type="success" icon="el-icon-edit" @click="$router.push('/questions/new')">新增试题</el-button>
          </el-col>
        </el-row>
        <!-- 表单 :label-position="labelPosition"文本对齐 -->
        <el-form style="margin-top:15px" :model="form" :label-position="labelPosition" label-width="80px"  >
          <el-row>
            <el-col :span="6">
              <el-form-item label="学科">
                <el-select v-model="form.subjectID"  placeholder="请选择" @change="selectSubject">
                 <el-option v-for="item in subjectList" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="二级目录" >
                <el-select v-model="form.catalogID"  placeholder="请选择">
                  <el-option v-for="item in catalogList" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="标签">
                <el-select v-model="form.tags"  placeholder="请选择">
                  <el-option v-for="item in tagList" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="关键字">
                <el-input v-model="form.keyword"  placeholder="根据题干搜索"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="试题类型">
                <el-select v-model="form.questionType"  placeholder="请选择">
                  <el-option v-for="item in questionType" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="难度">
                <el-select v-model="form.difficulty" placeholder="请选择">
                  <el-option v-for="item in difficulty" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="方向">
                <el-select v-model="form.direction" placeholder="请选择">
                  <el-option  v-for="item in direction" :key="item" :label="item" :value="item"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="录入人">
                <el-select v-model="form.creatorID" placeholder="请选择">
                  <el-option v-for="item in creatorList" :key="item.id" :label="item.username" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="题目备注">
                <el-input v-model="form.remarks"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="企业简称">
               <el-input v-model="form.shortName" ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="城市">
                <el-row type="flex" justify="space-between">
                  <el-col :span="11">
                    <el-select v-model="form.province" placeholder="请选择" @change="changeProvinces">
                      <el-option v-for="item in provinces" :key="item" :label="item" :value="item"></el-option>
                    </el-select>
                  </el-col>
                  <el-col :span="12">
                    <el-select v-model="form.city" placeholder="请选择">
                      <el-option v-for="item in citys" :key="item" :label="item" :value="item"></el-option>
                    </el-select>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item style="float:right">
                <el-button size="small" @click="clearForm">清除</el-button>
                <el-button size="small" type="primary" @click="sureClickSearch">搜索</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <!-- tab栏切换 -->
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
          <el-tab-pane label="全部" name="all"></el-tab-pane>
          <el-tab-pane label="待审核" name="checking"></el-tab-pane>
          <el-tab-pane label="已审核" name="checked"></el-tab-pane>
          <el-tab-pane label="已拒绝" name="reject"></el-tab-pane>
        </el-tabs>
        <!-- 警告：总条数  :closable 是否可关闭-->
        <el-alert :title="title" type="info" show-icon :closable="false"></el-alert>
        <!-- 表格 -->
        <el-table
          style="width: 100%; margin-top:15px; "
          :header-cell-style="{ background:'#fafafa'}"
          :data="choiceList">
          <el-table-column prop="number" label="试题编号" width="120"></el-table-column>
          <el-table-column prop="subject" label="学科" width="120"></el-table-column>
          <el-table-column prop="catalog" label="目录" width="120"></el-table-column>
          <el-table-column prop="questionType" label="题型" width="120" :formatter="formatType"></el-table-column>
          <el-table-column prop="question" v-html="choiceList.question" label="题干" width="280">
            <template #default="scope">
              <span v-html="scope.row.question"></span>
            </template>
          </el-table-column>
          <el-table-column prop="addDate" label="录入时间" width="180">
            <template #default="scope">
              {{formatDate(scope.row.addDate)}}
            </template>
          </el-table-column>
          <el-table-column prop="difficulty" label="难度" width="80" :formatter="formatDifficulty"></el-table-column>
          <el-table-column prop="creator" label="录入人" width="120"></el-table-column>
          <el-table-column prop="chkState" label="审核状态" width="120" :formatter="formatState"></el-table-column>
          <el-table-column prop="chkRemarks" label="审核意见" width="145"></el-table-column>
          <el-table-column prop="chkUser" label="审核人" width="120"></el-table-column>
          <el-table-column prop="publishState" label="发布状态" width="120" :formatter="formatPstate"></el-table-column>
          <el-table-column align="center" fixed="right" label="操作" width="200">
            <template #default="scope">
              <el-button type="text" size="small" @click="previewQuesion(scope.row)">预览</el-button>
              <el-button type="text" size="small" :disabled="scope.row.chkState === 0 ? false : true" @click="chkQuestion(scope.row)">审核</el-button>
              <el-button type="text" size="small" :disabled="scope.row.publishState === 0 ? false : true" @click="jumpEdit(scope.row.id)">修改</el-button>
              <el-button type="text" size="small" @click="upAndDown(scope.row)">{{scope.row.publishState === 0 ? '上架' : '下架'}}</el-button>
              <el-button type="text" size="small" :disabled="scope.row.publishState === 0 ? false : true" @click="delQuestion(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          style="margin-top:20px;display:flex;justify-content:end"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="form.page"
          :page-sizes="[ 5, 10, 20, 30]"
          :page-size="form.pagesize"
          layout="prev, pager, next, sizes, jumper"
          :total="totals">
        </el-pagination>
      </el-card>
      <questionsCheck :isShowCheckDialog.sync="isShowCheckDialog" :chkData="chkData"></questionsCheck>
      <questionsPreview :isShowDialog.sync="isShowDialog" :topicData="topicData" :isVideo.sync="isVideo" :optionsList="optionsList"></questionsPreview>
    </div>
  </div>
</template>

<script>
// 学科
import { simple as subjectSimple } from '@/api/hmmm/subjects.js'
// 目录
import { simple as directorySimple } from '@/api/hmmm/directorys.js'
// 标签
import { simple as tagSimple } from '@/api/hmmm/tags.js'
// 枚举
import { questionType, difficulty, chkType, publishType, direction } from '@/api/hmmm/constants.js'
// 省市
import { provinces, citys } from '@/api/hmmm/citys.js'
// 录入人
import { simple as userSimple } from '@/api/base/users.js'
// 精选题库列表
import { choice, remove, choicePublish, detail } from '@/api/hmmm/questions.js'
import dayjs from 'dayjs'
// 预览弹窗
import questionsPreview from '@/module-hmmm/components/questions-preview.vue'
// 审核意见弹窗
import questionsCheck from '@/module-hmmm/components/questions-check.vue'

export default {
  data () {
    return {
      labelPosition: 'right', // 表单域文本右对齐
      subjectList: [], // 学科列表
      catalogList: [], // 二级目录
      tagList: [], // 标签
      // 枚举映射
      questionType: questionType,
      difficulty: difficulty,
      direction: direction,
      creatorList: [], // 录入人列表
      provinces: [], // 省市
      citys: [], // 区
      form: {
        page: 1,
        pagesize: 5,
        subjectID: null, // 学科
        catalogID: null, // 二级目录
        tags: null, // 标签
        keyword: null, // 关键字
        questionType: null, // 试题类型
        difficulty: null, // 难度
        direction: null, // 方向
        creatorID: null, // 录入人
        remarks: null, // 题目备注
        shortName: null, // 企业简称
        province: null, // 城市
        city: null, // 乡区
        chkState: null // 审核状态
      },
      activeName: 'all', // tab栏默认选中第一个
      totals: 0, // 总条数
      choiceList: [], // 精选题库
      isShowDialog: false, // 预览弹窗
      isVideo: false, // 预览弹窗视频
      topicData: {}, // 预览当前数据
      optionsList: [], // 预览答案
      isShowCheckDialog: false, // 审核弹窗
      chkData: {}, // 审核数据
      upDownState: '' // 上下架
    }
  },
  components: {
    questionsCheck, // 审核弹窗
    questionsPreview // 预览弹窗
  },
  computed: {
    title () {
      return `数据一共 ${this.totals} 条`
    }
  },
  created () {
    this.getSubject() // 获取学科
    this.getCreator() // 获取录入人
    this.provinces = provinces() // 获取省市
    this.choiceQuestions() // 获取精选题库列表
  },
  methods: {
    // 获取学科
    async getSubject () {
      const { data } = await subjectSimple()
      // console.log(data)
      this.subjectList = data
    },
    // 选择学科
    async selectSubject (value) {
      // console.log(value) // 当前点击的学科的id
      // 二级目录和标签 与 学科 的联动 学科发生改变的时候为空
      this.form.catalogID = ''
      this.form.tags = ''
      const { data } = await directorySimple({ subjectID: value })
      this.catalogList = data
      const res = await tagSimple({ subjectID: value })
      // console.log(res)
      this.tagList = res.data
    },
    // 获取录入人
    async getCreator () {
      const { data } = await userSimple()
      // console.log(data)
      this.creatorList = data
    },
    // 获取省市
    changeProvinces (val) {
      // console.log(val)
      // 省市区联动 省市改变的时候区为空
      this.form.city = ''
      this.citys = citys(val)
    },
    // 确定搜索
    sureClickSearch () {
      this.form.page = 1
      this.choiceQuestions()
    },
    // 清除
    clearForm () {
      this.form = {
        page: 1,
        pagesize: 5,
        subjectID: null, // 学科
        catalogID: null, // 二级目录
        tags: null, // 标签
        keyword: null, // 关键字
        questionType: null, // 试题类型
        difficulty: null, // 难度
        direction: null, // 方向
        creatorID: null, // 录入人
        remarks: null, // 题目备注
        shortName: null, // 企业简称
        province: null, // 城市
        city: null, // 乡区
        chkState: null
      }
      this.catalogList = [] // 清空二级目录
      this.choiceQuestions()
    },
    // 处理tab栏
    async handleClick (tab, event) {
      // console.log(tab.name) // 点击的名字
      if (tab.name === 'all') {
        this.form.chkState = null
        this.form.page = 1
        const { data } = await choice(this.form)
        // console.log(data)
        this.choiceList = data.items
        this.totals = data.counts
      } else if (tab.name === 'checking') {
        this.form.chkState = 0
        this.form.page = 1
        const { data } = await choice(this.form)
        // console.log(data)
        this.choiceList = data.items
        this.totals = data.counts
      } else if (tab.name === 'checked') {
        this.form.chkState = 1
        this.form.page = 1
        const { data } = await choice(this.form)
        // console.log(data)
        this.choiceList = data.items
        this.totals = data.counts
      } else if (tab.name === 'reject') {
        this.form.chkState = 2
        this.form.page = 1
        const { data } = await choice(this.form)
        // console.log(data)
        this.choiceList = data.items
        this.totals = data.counts
      }
    },
    // 获取精选题库列表
    async choiceQuestions () {
      const { data } = await choice(this.form)
      this.choiceList = data.items
      this.totals = data.counts
    },
    // 格式化题型
    formatType (row, column, cellValue, index) {
      const result = questionType.find((item) => {
        return item.value === Number(cellValue)
      })
      return result?.label
    },
    // 格式化难度
    formatDifficulty (row, column, cellValue, index) {
      const result = difficulty.find((item) => {
        return item.value === Number(cellValue)
      })
      return result?.label
    },
    // 格式化审核状态
    formatState (row, column, cellValue, index) {
      const result = chkType.find((item) => {
        return item.value === Number(cellValue) + 1
      })
      return result?.label
    },
    // 格式化发布状态
    formatPstate (row, column, cellValue, index) {
      const result = publishType.find((item) => {
        return item.value === Number(cellValue) + 1
      })
      return result?.label
    },
    // 格式化时间
    formatDate (time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    },
    // 删除精选问题
    delQuestion (data) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(data)
        if (this.choiceList.length === 1 && this.form.page > 1) {
          this.form.page--
        }
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.choiceQuestions()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 审核精选问题
    chkQuestion (row) {
      this.isShowCheckDialog = true
      this.chkData = row
    },
    // 预览精选问题
    async previewQuesion (row) {
      this.isShowDialog = true
      const { data } = await detail(row)
      // console.log(data)
      this.topicData = data
      this.optionsList = data.options
    },
    // 修改精选题库
    jumpEdit (id) {
      this.$router.push(`new?id=${id}`)
    },
    // 上下架精选问题
    upAndDown (row) {
      this.upDownState = row.publishState === 0 ? '上架' : '下架'
      this.$confirm(`您确认${this.upDownState}这道题目吗?`, '提示', {
        type: 'warning'
      }).then(async () => {
        row.publishState = !row.publishState
        await choicePublish({ id: row.id, publishState: Number(row.publishState) })
        this.$message({
          type: 'success',
          message: `恭喜，${this.upDownState}成功!`
        })
        this.choiceQuestions()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消该操作'
        })
      })
    },
    // 分页
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.form.pagesize = val
      this.choiceQuestions()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.form.page = val
      this.choiceQuestions()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select{
  width: 100%;
}
</style>
